package com.myrobot.util;

import java.awt.AWTException;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

public class RobotUtil {
	private static Robot robot = null;

	static {
		try {
			robot = new Robot();
		} catch (AWTException e) {
			System.out.println("Internal Error: " + e);
			e.printStackTrace();
		}
	}

	public static void delay(int ms) {
		robot.delay(ms);
	}

	public static void mouseWheel(int wheelAmt) {
		robot.mouseWheel(5);
	}

	public static void mouseMove(int x, int y) {
		robot.mouseMove(x, y);
	}

	public static void mousePress(int key) {
		robot.mousePress(key);
	}

	public static void mouseRelease(int key) {
		robot.mouseRelease(key);
	}

	public static void keyPress(int... keys) {
		int[] arrayOfInt = keys;
		int j = keys.length;
		for (int i = 0; i < j; i++) {
			int key = arrayOfInt[i];
			robot.keyPress(key);
		}
	}

	public static void keyRelease(int... keys) {
		int[] arrayOfInt = keys;
		int j = keys.length;
		for (int i = 0; i < j; i++) {
			int key = arrayOfInt[i];
			robot.keyRelease(key);
		}
	}

	public static void keyClick(int... keys) {
		keyPress(keys);
		robot.delay(50);
		keyRelease(keys);
		robot.delay(50);
	}

	public static void keyRepeat(int key, int count) {
		for (int i = 0; i < count; i++) {
			keyClick(new int[] { key });
			robot.delay(50);
		}
	}

	public static void input(String format, Object... args) {
		String text = String.format(format, args);
		for (int i = 0; i < text.length(); i++) {
			char character = text.charAt(i);
			type(character);
		}
	}

	public static void type(char character) {
		switch (character) {
		case 'a':
			keyClick(new int[] { 65 });
			break;
		case 'b':
			keyClick(new int[] { 66 });
			break;
		case 'c':
			keyClick(new int[] { 67 });
			break;
		case 'd':
			keyClick(new int[] { 68 });
			break;
		case 'e':
			keyClick(new int[] { 69 });
			break;
		case 'f':
			keyClick(new int[] { 70 });
			break;
		case 'g':
			keyClick(new int[] { 71 });
			break;
		case 'h':
			keyClick(new int[] { 72 });
			break;
		case 'i':
			keyClick(new int[] { 73 });
			break;
		case 'j':
			keyClick(new int[] { 74 });
			break;
		case 'k':
			keyClick(new int[] { 75 });
			break;
		case 'l':
			keyClick(new int[] { 76 });
			break;
		case 'm':
			keyClick(new int[] { 77 });
			break;
		case 'n':
			keyClick(new int[] { 78 });
			break;
		case 'o':
			keyClick(new int[] { 79 });
			break;
		case 'p':
			keyClick(new int[] { 80 });
			break;
		case 'q':
			keyClick(new int[] { 81 });
			break;
		case 'r':
			keyClick(new int[] { 82 });
			break;
		case 's':
			keyClick(new int[] { 83 });
			break;
		case 't':
			keyClick(new int[] { 84 });
			break;
		case 'u':
			keyClick(new int[] { 85 });
			break;
		case 'v':
			keyClick(new int[] { 86 });
			break;
		case 'w':
			keyClick(new int[] { 87 });
			break;
		case 'x':
			keyClick(new int[] { 88 });
			break;
		case 'y':
			keyClick(new int[] { 89 });
			break;
		case 'z':
			keyClick(new int[] { 90 });
			break;
		case 'A':
			keyClick(new int[] { 16, 65 });
			break;
		case 'B':
			keyClick(new int[] { 16, 66 });
			break;
		case 'C':
			keyClick(new int[] { 16, 67 });
			break;
		case 'D':
			keyClick(new int[] { 16, 68 });
			break;
		case 'E':
			keyClick(new int[] { 16, 69 });
			break;
		case 'F':
			keyClick(new int[] { 16, 70 });
			break;
		case 'G':
			keyClick(new int[] { 16, 71 });
			break;
		case 'H':
			keyClick(new int[] { 16, 72 });
			break;
		case 'I':
			keyClick(new int[] { 16, 73 });
			break;
		case 'J':
			keyClick(new int[] { 16, 74 });
			break;
		case 'K':
			keyClick(new int[] { 16, 75 });
			break;
		case 'L':
			keyClick(new int[] { 16, 76 });
			break;
		case 'M':
			keyClick(new int[] { 16, 77 });
			break;
		case 'N':
			keyClick(new int[] { 16, 78 });
			break;
		case 'O':
			keyClick(new int[] { 16, 79 });
			break;
		case 'P':
			keyClick(new int[] { 16, 80 });
			break;
		case 'Q':
			keyClick(new int[] { 16, 81 });
			break;
		case 'R':
			keyClick(new int[] { 16, 82 });
			break;
		case 'S':
			keyClick(new int[] { 16, 83 });
			break;
		case 'T':
			keyClick(new int[] { 16, 84 });
			break;
		case 'U':
			keyClick(new int[] { 16, 85 });
			break;
		case 'V':
			keyClick(new int[] { 16, 86 });
			break;
		case 'W':
			keyClick(new int[] { 16, 87 });
			break;
		case 'X':
			keyClick(new int[] { 16, 88 });
			break;
		case 'Y':
			keyClick(new int[] { 16, 89 });
			break;
		case 'Z':
			keyClick(new int[] { 16, 90 });
			break;
		case '`':
			keyClick(new int[] { 192 });
			break;
		case '0':
			keyClick(new int[] { 48 });
			break;
		case '1':
			keyClick(new int[] { 49 });
			break;
		case '2':
			keyClick(new int[] { 50 });
			break;
		case '3':
			keyClick(new int[] { 51 });
			break;
		case '4':
			keyClick(new int[] { 52 });
			break;
		case '5':
			keyClick(new int[] { 53 });
			break;
		case '6':
			keyClick(new int[] { 54 });
			break;
		case '7':
			keyClick(new int[] { 55 });
			break;
		case '8':
			keyClick(new int[] { 56 });
			break;
		case '9':
			keyClick(new int[] { 57 });
			break;
		case '-':
			keyClick(new int[] { 45 });
			break;
		case '=':
			keyClick(new int[] { 61 });
			break;
		case '~':
			keyClick(new int[] { 16, 192 });
			break;
		case '!':
			keyClick(new int[] { 517 });
			break;
		case '@':
			keyClick(new int[] { 512 });
			break;
		case '#':
			keyClick(new int[] { 520 });
			break;
		case '$':
			keyClick(new int[] { 515 });
			break;
		case '%':
			keyClick(new int[] { 16, 53 });
			break;
		case '^':
			keyClick(new int[] { 514 });
			break;
		case '&':
			keyClick(new int[] { 150 });
			break;
		case '*':
			keyClick(new int[] { 151 });
			break;
		case '(':
			keyClick(new int[] { 519 });
			break;
		case ')':
			keyClick(new int[] { 522 });
			break;
		case '_':
			keyClick(new int[] { 523 });
			break;
		case '+':
			keyClick(new int[] { 521 });
			break;
		case '\t':
			keyClick(new int[] { 9 });
			break;
		case '\n':
			keyClick(new int[] { 10 });
			break;
		case '[':
			keyClick(new int[] { 91 });
			break;
		case ']':
			keyClick(new int[] { 93 });
			break;
		case '\\':
			keyClick(new int[] { 92 });
			break;
		case '{':
			keyClick(new int[] { 16, 91 });
			break;
		case '}':
			keyClick(new int[] { 16, 93 });
			break;
		case '|':
			keyClick(new int[] { 16, 92 });
			break;
		case ';':
			keyClick(new int[] { 59 });
			break;
		case ':':
			keyClick(new int[] { 513 });
			break;
		case '\'':
			keyClick(new int[] { 222 });
			break;
		case '"':
			keyClick(new int[] { 152 });
			break;
		case ',':
			keyClick(new int[] { 44 });
			break;
		case '<':
			keyClick(new int[] { 153 });
			break;
		case '.':
			keyClick(new int[] { 46 });
			break;
		case '>':
			keyClick(new int[] { 160 });
			break;
		case '/':
			keyClick(new int[] { 47 });
			break;
		case '?':
			keyClick(new int[] { 16, 47 });
			break;
		case ' ':
			keyClick(new int[] { 32 });
			break;
		case '\013':
		case '\f':
		case '\r':
		case '\016':
		case '\017':
		case '\020':
		case '\021':
		case '\022':
		case '\023':
		case '\024':
		case '\025':
		case '\026':
		case '\027':
		case '\030':
		case '\031':
		case '\032':
		case '\033':
		case '\034':
		case '\035':
		case '\036':
		case '\037':
		default:
			throw new IllegalArgumentException("Cannot type character " + character);
		}

	}

	public static void leftMouseDoubleClick(int x, int y) {
		robot.mouseMove(x, y);
		robot.mousePress(16);
		robot.delay(50);
		robot.mouseRelease(16);
		robot.delay(50);
		robot.mousePress(16);
		robot.delay(50);
		robot.mouseRelease(16);
		robot.delay(50);
	}

	public static void leftMouseDoubleClick(Point p) {
		leftMouseDoubleClick(p.x, p.y);
	}

	public static void leftMouseClick(int x, int y) {
		robot.mouseMove(x, y);
		robot.mousePress(16);
		robot.delay(10);
		robot.mouseRelease(16);
		robot.delay(10);
	}

	public static void leftMouseClick(Point p) {
		leftMouseClick(p.x, p.y);
	}

	public static void rightMouseClick(int x, int y) {
		robot.mouseMove(x, y);
		robot.mousePress(4);
		robot.delay(10);
		robot.mouseRelease(4);
		robot.delay(10);
	}

	public static void rightMouseClick(Point p) {
		rightMouseClick(p.x, p.y);
	}

	public static void pressKeys(int[] ks) {
		for (int i = 0; i < ks.length; i++) {
			robot.keyPress(ks[i]);
			robot.delay(50);
			robot.keyRelease(ks[i]);
			robot.delay(50);
		}
	}

	public static void doCopy() {
		robot.setAutoDelay(200);
		robot.keyPress(17);
		robot.keyPress(67);
		robot.keyRelease(17);
		robot.keyRelease(67);
	}

	public static void doParse() {
		robot.setAutoDelay(500);
		robot.mouseMove(300, 300);
		robot.mousePress(16);
		robot.mouseRelease(16);
		robot.keyPress(17);
		robot.keyPress(86);
		robot.keyRelease(17);
		robot.keyRelease(86);
	}

	public static BufferedImage captureFullScreen() {
		BufferedImage fullScreenImage = robot
				.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
		return fullScreenImage;
	}

	public static BufferedImage capturePartScreen(int x, int y, int width, int height) {
		robot.mouseMove(x, y);
		return robot.createScreenCapture(new Rectangle(width, height));
	}
}

/*
 * Location: F:\runnable\yijieRobot.jar!\com\yijierobot\RobotUtil.class Java
 * compiler version: 8 (52.0) JD-Core Version: 0.7.1
 */